<template>
  <div class="login-page">
    <div class="login-box">
      <!-- 头像区域 -->
      <div class="toux-box">
        <img src="http://weldon.net.cn/back/img/logo2.fc048282.png" alt="" />
      </div>
      <!-- 登录表单区域 -->
      <el-form class="login-from" ref="form" :model="form" :rules="rules">
        <el-form-item prop="username">
          <el-input
            v-model="form.username"
            prefix-icon="el-icon-user-solid"
          ></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            type="password"
            v-model="form.password"
            prefix-icon="el-icon-lock"
          ></el-input>
        </el-form-item>
        <el-form-item class="btn">
          <el-button type="primary" @click="submit" :plain="true"
            >登录</el-button
          >
          <el-button @click="reset">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import { loginapi } from "@/api/user";
export default {
  name: "LoginIndex",
  methods: {
    async submit() {
      try {
        //validate当你输入内容 或 点击提交 都会校验输入的数据是否符合表单验证的要求
        const yes = await this.$refs.form.validate();

        if (yes) {
          const res = await loginapi(this.form);
          //获取响应回来数据 token存起来
          console.log(res.data.token);
          this.$store.commit("user/setUserToken", res.data.token),
            //跳转页面
            this.$router.push("/");
          this.$message({
            showClose: true,
            message: "登录成功",
            type: "success",
          });
          console.log(res);
        }
      } catch (err) {
        console.log("失败");
      }
    },
    reset() {
      this.$refs.form.resetFields();
    },
  },
  data() {
    return {
      form: {
        username: "admin",
        password: "123456",
      },
      rules: {
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" },
          {
            min: 3,
            max: 10,
            trigger: "blur",
            message: "长度在 3 到 10 个字符",
          },
        ],
        password: [
          { required: true, message: "请输入密码", trigger: "blur" },
          {
            min: 6,
            max: 15,
            message: "长度在 6 到 15 个字符",
            trigger: "blur",
          },
        ],
      },
    };
  },
};
</script>

<style lang="less" scoped>
.login-page {
  position: fixed;
  background-color: #2b4b6b;
  height: 100%;
  width: 100%;

  .login-box {
    width: 450px;
    height: 300px;
    background-color: #fff;
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
    .toux-box {
      height: 130px;
      width: 130px;
      border: 1px solid #eee;
      border-radius: 50%;
      padding: 5px;
      box-shadow: 0 0 10px #ddd;
      margin: 0 auto;
      transform: translateY(-50%);
      background-color: #fff;
      img {
        width: 100%;
        height: 100%;
        border-radius: 50%;
        background-color: #eee;
      }
    }
    .login-from {
      position: absolute;
      bottom: 0;
      width: 100%;
      padding: 0 20px;
      box-sizing: border-box;
      .btn {
        display: flex;
        justify-content: flex-end;
      }
      .el-form-item {
        margin-bottom: 22px;
      }
    }
  }
}
</style>
